<?php
/**
 * 职位管理
 * jzzmly
 */
class EAdmin_Page_JobManage extends EAdmin_Page_Abstract
{
	private $resumeDb;
	private function loadDb()
	{
		$this->resumeDb = Db_Resume::instance();
	}
	public function validate(ME_Request $input, ME_Response $output)
	{
		$euserInfo = Libs_EUser::getLoginStatus($input,$output);
		//get my section
		$mySectionArr = Libs_EUser::getUserSection($euserInfo);
		$output->mySectionArr = $mySectionArr;
		$output->euid = 
			$input->euid =
			$euid = $euserInfo[0];

		if(empty($euid)){
			header("location:index.php?c=login");
		}

		$state = ME_Config::get("ResumeState");
		$output->state = $state['state'];
		$output->eduDegree = $eduDegree = $state['eduDegree'];

		$dataArr = Libs_Admin_Category::getCategoryList(array('module_type'=>1,'subclassid'=>0,'type'=>1,'pagesize'=>1000));
		$newDataArr = array();
		if($dataArr){
			foreach($dataArr as $data){
				$id = !empty($data['id']) ? $data['id'] : 0;
				if($id == 0){
					continue;
				}
				$newDataArr[$id] = $data['name'];
			}
		}
		$output->cityArr = $newDataArr;


		$euid && $stateinfo = Libs_EState::getEResumeState($euid);
		if(!empty($stateinfo)){
			$state_arr = explode(",",$stateinfo);
		}else{
			$state_arr = array();
		}

		$output->state_arr = $state_arr;


		return true;
	}

	public function doDefault(ME_Request $input, ME_Response $output)
	{
		die('错误的请求');
	}
	
	
	//get list
	public function doList(ME_Request $input, ME_Response $output)
	{
		$condition = " and firm_id='{$output->euid}'";
		//部门的限制
		$sectionStr = $output->mySectionArr ? implode(',',array_keys($output->mySectionArr)) : '';
		$sectionStr && $condition .= " and section_id in({$sectionStr})";
		
		$db = Db_Resume::instance();
		$output->jobStatusArr = 
		$jobStatusArr = array('1'=>'启动','2'=>'暂停');
		
		$pageSize = 2;
		$page = $input->get("page");
		if(empty($page)){
			$page = 1;
		}
		$from = ($page-1)*$pageSize;
		
		//all submit deal
		$idArr = $input->post('id');
		if($idArr){
			$more .= ' and id in('.implode(',',$idArr).')';
			//delete
			if($input->post('delBtn')){
				$db->query("Update `e_job` Set is_del=1 Where 1 {$condition} {$more}");
			}
			//publish
			if($input->post('pubBtn')){
				$db->query("Update `e_job` Set is_publish=1 Where 1 {$condition} {$more}");
			}
			//set status
			$setStatus = intval($input->post('setStatus'));
			if($setStatus){
				$db->query("Update `e_job` Set `status`='{$setStatus}' Where 1 {$condition} {$more}");
			}
		}

		//条件内容
		$pageUrl = $input->baseUrl.'/index.php?c=JobManage&a=List';
		
		$name = trim($input->request('name'));
		if(!empty($name)){
			$output->name = $name;
			$condition .= 'and name like "%'.$name.'%" ';
			$pageUrl .= "&name={$name}";
		}
		
		$classId = intval($input->request('class_id'));
		if($classId){
			$condition .= " and class_id='{$classId}'";
			$pageUrl .= "&class_id={$classId}";
		}
		//class
		$output->jobClassArr = 
		$jobClassArr = $this->_getFirmJobClassArr($output->euid);
		$output->jobClassStr = Libs_GlobalFunc::createSelectElement($jobClassArr,'class_id','class_id','所属职位分类',$classId);
		
		//status
		$status = intval($input->request('status'));
		if($status){
			$condition .= " and status='{$status}'";
			$pageUrl .= "&status={$status}";
		}
		$output->jobStatusStr = Libs_GlobalFunc::createSelectElement($jobStatusArr,'status','status','职位状态',$status);
		
		//pageing
		$totalNum = $db->getOne("Select count(*) From `e_job` Where is_del=0 {$condition}");
		$output->pageStr = Libs_GlobalFunc::multiPage(array(
		'page' => $page,
		'pageSize' => $pageSize,
		'total' => $totalNum,
		'baseUrl' => $pageUrl."&page="
		));
	
		$sql = "Select * From `e_job` Where 1 and is_del=0 $condition limit {$from},{$pageSize}";
//echo $sql;
		$output->dataList =
			$dataList = $db->getAll($sql);

		$output->setTemplate("JobList");
	}
	
	//add 
	public function doAdd(ME_Request $input, ME_Response $output)
	{
		$condition = " and firm_id='{$output->euid}'";
		//post
		if($input->post('addJob')){
			$addParam = '';
			$now = time();
			$addParam['firm_id'] = $output->euid;
			$addParam['name'] = trim($input->post('name'));
			$addParam['en_name'] = trim($input->post('en_name'));
			$addParam['class_id'] = intval($input->post('class_id'));
			$addParam['section_id'] = intval($input->post('section_id'));
			$addParam['province_id'] = intval($input->post('province_id'));
			$addParam['city_id'] = intval($input->post('city_id'));
			$addParam['salary'] = trim($input->post('salary'));
			$addParam['need_num'] = intval($input->post('need_num'));
			$addParam['sort_order'] = intval($input->post('sort_order'));
			$addParam['digest'] = trim($input->post('digest'));
			$addParam['add_time'] = $now;
			$addParam['update_time'] = $now;
			
			
			//add
			$insertId = Libs_EDbHand::addEJob(array('arr'=>$addParam));
		    if($insertId){
		    	self::showMsg('添加成功','/index.php?c=JobManage&a=List');
		    } else {
		    	self::showMsg('网络连接失败，请稍候再试','index.php?c=JobManage&a=Add');
		    }
		}

		//得到分类
		$jobClassArr = $this->_getFirmJobClassArr($output->euid);
		$output->jobClassStr = Libs_GlobalFunc::createSelectElement($jobClassArr,'class_id','class_id','选择职位分类');
		//得到部门
		$output->mySectionStr = Libs_GlobalFunc::createSelectElement($output->mySectionArr,'section_id','section_id','选择职位部门');
		//省份列表
		$output->provOptions = Libs_Admin_Module::getModuleSelect(array('selVal'=>0,'module_type'=>1,'subclassid'=>0,'type'=>1,'pagesize'=>1000));
		//城市列表
		$output->cityOptions = Libs_Admin_Module::getModuleSelect(array('selVal'=>0,'module_type'=>1,'classid'=>0,'type'=>2,'pagesize'=>1000));
		
		$output->setTemplate("JobAdd");
	}
	
	//edit
	public function doEdit(ME_Request $input, ME_Response $output)
	{
		$id = $input->request('id'); //编辑的id
		if(!$id) die('您要请求的内容不存在');

		$condition = " and firm_id='{$output->euid}' and id='{$id}'";
		//部门的限制
		$sectionStr = implode(',',array_keys($output->mySectionArr));
		$condition .= " and section_id in({$sectionStr})";
		
		$db = Db_Resume::instance();
		$sql = "Select * From `e_job` Where 1 {$condition} ";
		$dataInfo = $db->getRow($sql);
		if(!$dataInfo) die('您要请求的内容不存在');

		//post
		if($input->post('updateSubmit')){
			$addParam = '';
			$addParam['name'] = trim($input->post('name'));
			$addParam['en_name'] = trim($input->post('en_name'));
			$addParam['section_id'] = intval($input->post('section_id'));
			$addParam['class_id'] = intval($input->post('class_id'));
			$addParam['province_id'] = intval($input->post('province_id'));
			$addParam['city_id'] = intval($input->post('city_id'));
			$addParam['salary'] = trim($input->post('salary'));
			$addParam['need_num'] = intval($input->post('need_num'));
			$addParam['sort_order'] = intval($input->post('sort_order'));
			$addParam['digest'] = trim($input->post('digest'));
			$addParam['update_time'] = time();

			//update
			$res = Libs_EDbHand::updateEJob(array('arr'=>$addParam,'where'=>" where id='{$id}' "));
			if($res){
				self::showMsg('更新成功','/index.php?c=JobManage&a=List');
			} else {
				self::showMsg('网络连接失败，请稍候再试','index.php?c=JobManage&a=Edit&id='.$id);
			}
			die();
		}

		//赋值
		$output->dataInfo = $dataInfo;
		//得到分类
		$jobClassArr = $this->_getFirmJobClassArr($output->euid);
		$output->jobClassStr = Libs_GlobalFunc::createSelectElement($jobClassArr,'class_id','class_id','选择职位分类',$dataInfo['class_id']);
		//得到部门
		$output->mySectionStr = Libs_GlobalFunc::createSelectElement($output->mySectionArr,'section_id','section_id','选择职位部门',$dataInfo['section_id']);
		//省份列表
		$output->provOptions = Libs_Admin_Module::getModuleSelect(array('selVal'=>$dataInfo['province_id'],'module_type'=>1,'subclassid'=>0,'type'=>1,'pagesize'=>1000));
		//城市列表
		$output->cityOptions = Libs_Admin_Module::getModuleSelect(array('selVal'=>$dataInfo['city_id'],'module_type'=>1,'classid'=>$dataInfo['province_id'],'type'=>2,'pagesize'=>1000));

		$output->setTemplate("JobEdit");
	}
	
	
	
	
	
	
	//private function
	
	private function _getFirmJobClassArr($firmId)
	{
		if(!$firmId) return '';
		$outArr = '';
		$db = Db_Resume::instance();
		$sql = "Select id,name,en_name From `e_job_class` Where is_del=0 and firm_id='{$firmId}'";
		$data = $db->getAll($sql);
		if($data){
			foreach ($data as $val) {
				$outArr[$val['id']] = $val['name'];
			}
		}
		return $outArr;
	}
	
	
	

}
